package stars.starSystems;

import gameAppLayer.enums.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.IModel;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.Mesh.Mode;
import com.jme3.scene.control.LodControl;
import com.jme3.scene.shape.Quad;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;

import controller.AbstractController;
import controller.IController;
import controls.RotationControl;

import stars.JMEControls.StarSystemViewControl;
import stars.enums.StarSystemDistributions;
import tiles.HexMeshBuilder;
import tiles.HexMeshBuilder2;
import utilities.CoordConvert;
import utilities.CustomMesh;
import view.EViewList;
import view.AbstractView;
import view.IView;

/*
 * Constructs VStarSystem views of MStarSystem objects
 */
public class StarSystemViewBuilder {
	
	private static final Logger logger = Logger.getLogger(StarSystemViewBuilder.class.getName());
	
	public Spatial buildView(AssetManager assetManager, StarSystemModel model) {
		
		logger.log(Level.INFO, "buildView");
		
		//Generate object method	
//		HexMeshBuilder2 hexBuild = new HexMeshBuilder2();
//		Mesh mesh = hexBuild.buildMesh();
//		
//		Geometry geometry = new Geometry("test", mesh);
//		
//		Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//		mat.setColor("Color", ColorRGBA.Black);
//		//mat.setColor("GlowColor", ColorRGBA.Blue);
//		
////		Material mat = new Material(assets, "Common/MatDefs/Light/Lighting.j3md");
////		mat.setColor("Specular", ColorRGBA.Black);
////		mat.setFloat("Shininess", 50f);
//		
//		geometry.setMaterial(mat);
		
		
//		int vertsPerHex = 12; //6 lines * 2 verts per line
//		CustomMesh lineMesh = new CustomMesh(vertsPerHex);
//		lineMesh.setMode(Mode.Lines);
//		
//		float sideLength = (float) (1/Math.sqrt(3));
//		float xVal = 0.5f;
//		float yVal = sideLength;
//		
//		float tempX = 0.5f;
//		float tempY = sideLength;
//		
//		float centerX;
//		float centerY;
//		
//		//for (int i = 0; i < 0; i++) {
//		//for (Integer i = 0; i < hexTileLocations.size(); i++) {
//		ArrayList<Double> coords = CoordConvert.hex2Cart(model.getLocation());
//		//hexTileCartCoords.add(CoordConvert.hex2Cart(hexTileLocations.get(i)));
//
//		centerX = coords.get(0).floatValue();
//		centerY = coords.get(1).floatValue();
//
//		//			System.out.println("Coords: x= " + coords.get(0) + " y= " + coords.get(1));
//		//			System.out.println("TempX= " + tempX + " TempY= " + tempY);
//		//			//xVal = centerX + tempX;
//		//			//yVal = centerY + tempY;
//		//			System.out.println("xVal= "+ xVal + " yVal= " + yVal);
//
//
//		lineMesh.addVertex(centerX-xVal,centerY-yVal/2,0);
//		lineMesh.addVertex(centerX,centerY-yVal,0);
//
//		lineMesh.addVertex(centerX,centerY-yVal,0);//			
//		lineMesh.addVertex(centerX+xVal,centerY-yVal/2,0);
//
//		lineMesh.addVertex(centerX+xVal,centerY-yVal/2,0);//
//		lineMesh.addVertex(centerX+xVal,centerY+yVal/2,0);
//
//		lineMesh.addVertex(centerX+xVal,centerY+yVal/2,0);//
//		lineMesh.addVertex(centerX,centerY+yVal,0);
//
//		lineMesh.addVertex(centerX,centerY+yVal,0);//
//		lineMesh.addVertex(centerX-xVal,centerY+yVal/2,0);
//
//		lineMesh.addVertex(centerX-xVal,centerY+yVal/2,0);//
//		lineMesh.addVertex(centerX-xVal,centerY-yVal/2,0);//
//		//}
//		lineMesh.setLineWidth(3.0f);
//		lineMesh.finish();
//		lineMesh.setStatic();
//		
//		//String s = i.toString();
//		Geometry geo = new Geometry("test", lineMesh);
//		
//		Material mat = new Material(assets, "Common/MatDefs/Misc/Unshaded.j3md");
//		mat.setColor("Color", ColorRGBA.Blue);
//		mat.setColor("GlowColor", ColorRGBA.Blue);
//		
////		Material mat = new Material(assets, "Common/MatDefs/Light/Lighting.j3md");
////		mat.setColor("Specular", ColorRGBA.Black);
////		mat.setFloat("Shininess", 50f);
//		
//		geo.setMaterial(mat);
		
		
		
		
		
				
		//Add controls for this view
		//Rotation control
//		logger.log(Level.INFO, "build rotationControl");
//		RotationControl rotControl = new RotationControl(geometry);
//		rotControl.setRotationAxis(new Vector3f(0,0,1), 0.25f);
//		logger.log(Level.INFO, "finished rotationControl");
		
		//View change control
		//StarSystemViewControl viewControl = new StarSystemViewControl(geometry, model);
		
		
		
		
		return geometry;
	}
}



//-----
//Loading model method
//Spatial highStar = assets.loadModel(Constants.modelsPath + "/StarHighDetail.mesh.xml");
//
//material.setColor("Color", new ColorRGBA(1.0f, 0.0f, 0.0f,  0.1f));
//highStar.setMaterial(material);		
//highStar.rotate(1.5708f, 0, 0);
//
//StarSystemView defaultView = new StarSystemView(EViewList.DEFAULT);
//defaultView.setSpatial(highStar);
//views.put(EViewList.DEFAULT, defaultView);
//-----




		
		//views.put(defaultView.getName(), defaultView);
		//-----------------------------------------
		
		
		//-----------------------------------------
		//Create Default Far view
		//QQQ:  Move this to a Control.  In fact, just create a control and add it to the default geometry (above).  This
		//control should handle all view changes based on incoming events or model states.
//		StarSystemView defaultViewFar = new StarSystemView(EViewList.DEFAULT_FAR);
//		Material farMaterial = new Material(assets, Constants.unshadedMaterial);
//		
//		Texture farTexture = assets.loadTexture(Constants.texturesPath + "/StarFar.png");		
//		farMaterial.setTexture("ColorMap", farTexture);
//		//set material color?
//		
//		identifier = new String("FarStarSystem");
//		Quad farStar = new Quad(2.0f, 2.0f);
//		Geometry geometry2 = new Geometry(identifier, farStar);
//		geometry2.setMaterial(farMaterial);
//		geometry2.setLocalTranslation(-1.0f, -1.0f, 0f);
//		//rotate geometry?
//		defaultViewFar.setSpatial(geometry2);
//		
//		views.put(defaultViewFar.getName(), defaultViewFar);
		//-----------------------------------------
		
		
		
		
		//Continue...
		
		
		
		
		

